package com.examplemartin.jpademo.service.purin;

import com.examplemartin.jpademo.dao.PurInDetailDao;
import com.examplemartin.jpademo.pojo.po.repository.purchase.PurInDetailPO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PurInDetailService {

    @Resource
    private PurInDetailDao purInDetailDao;

    /**
     * 模糊查询 获取入库单
     */
 /*   // 01 采购订单名 and 采购入库单名
    public List<PurInPO> findAll(String purname,String inname){

        // 模糊查询
        // 模糊查询 01 sort
        Sort byPurName = Sort.by(Sort.Direction.ASC, "purPO.name");
        Sort bycreateTime = Sort.by(Sort.Direction.ASC, "createTime");
        Sort and = byPurName.and(bycreateTime);

        List<PurInPO> all = purInDao.findAll(new Specification<PurInPO>() {
            @Override
            public Predicate toPredicate(Root<PurInPO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Predicate predicate = null;

                if(!ValidateMember.isEmpty(purname)){
                    Path<PurPO> purPO = root.get("purPO");
                    Predicate predicatepurname= cb.like(purPO.get("name").as(String.class), purname);
                    if(predicate==null){
                        predicate = predicatepurname;
                    }else{
                        predicate = cb.and(predicate,predicatepurname);
                    }
                }

                if(!ValidateMember.isEmpty(inname)){
                    Path<Object> purinname = root.get("name");
                    Predicate predicateinname = cb.like(purinname.as(String.class), inname);
                    if(predicate==null){
                        predicate = predicateinname;
                    }else{
                        predicate = cb.and(predicate,predicateinname);
                    }
                }
                return predicate;
            }
        },and);
        // 返回模糊查询结果
        return all;
    }*/

 /*   // 保存菜单
    public PurInPO save(PurInPO purInPO){
        return purInDao.save(purInPO);
    }


    // 删除菜单
    public void delete(PurInPO purInPO){
         purInDao.delete(purInPO);
    }*/

    // 按id查找明细
    public PurInDetailPO findById(Long id){
        return purInDetailDao.findById(id).get();
    }

    // 保存
    public PurInDetailPO save(PurInDetailPO purInDetailPO){
        return purInDetailDao.save(purInDetailPO);
    }

    // 删除明细
    public void deleteById(Long id){
        purInDetailDao.deletePhy(id);
    }

    // 根据 采购订单 和 商品id 产生入库数量汇总
    public List<Object[]> sumGroupbyGoodidAndpurid(Long purid,Long goodid){
        return purInDetailDao.sumGroupbyGoodidAndpurid(purid,goodid);
    }

}
